package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.contants.SystemConstants;
import com.sangeng.dto.LinkDTO;
import com.sangeng.entity.Link;
import com.sangeng.entity.ResponseResult;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.mapper.Exception.SystemException;
import com.sangeng.mapper.LinkMapper;
import com.sangeng.service.LinkService;
import com.sangeng.util.BeanCopyUtils;
import com.sangeng.vo.LinkVo;
import com.sangeng.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2022-08-19 14:14:24
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {
   @Autowired
    LinkService linkService;

   public ResponseResult selectLinkList(){

        LambdaQueryWrapper<Link> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        linkLambdaQueryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);
        List<Link> list = list(linkLambdaQueryWrapper);
        List<LinkVo> collect = list.stream()
                .map(link -> BeanCopyUtils.copyBean(link, LinkVo.class))
                .collect(Collectors.toList());

      return ResponseResult.okResult(collect);

    }

//    @Override
//    public ResponseResult getLinkList(Integer pageNum, Integer pageSize) {
//
//        Page<Link> linkPage = new Page<Link>(pageNum, pageSize);
//
//        Page<Link> page = page(linkPage, new QueryWrapper<Link>());
//        long total = page.getTotal();
//        List<Link> records = page.getRecords();
//        PageVo pageVo = new PageVo(records, total);
//        return ResponseResult.okResult(pageVo);
//    }

    @Override
    public ResponseResult deleteLink(List<Long> idList) {
        idList.stream()
                .forEach(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) {
                        UpdateWrapper<Link> LinkUpdateWrapper = new UpdateWrapper<>();
                        LinkUpdateWrapper.eq("id",aLong).set("del_flag",1);
                        boolean update = update(LinkUpdateWrapper);
                        if(!update){
                            throw new SystemException(AppHttpCodeEnum.DELETE_ERROR);
                        }
                    }
                });
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getLinkById(Long id) {

        Link link = linkService.getBaseMapper().selectById(id);
        LinkVo linkVo = BeanCopyUtils.copyBean(link, LinkVo.class);

        return ResponseResult.okResult(linkVo);
    }

    @Override
    public ResponseResult getLinkListByRequire(Integer pageNum, Integer pageSize, String name, String status) {
        Page<Link> articlePage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Link> LinkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Link> eq = LinkLambdaQueryWrapper
                .like(StringUtils.hasText(name), Link::getName, name)
                .eq(StringUtils.hasText(status), Link::getStatus, status);

        // articleLambdaQueryWrapper.like();
        Page<Link> page = page(articlePage, LinkLambdaQueryWrapper);
        List<Link> articleList= page.getRecords();
        long total = page.getTotal();
        PageVo pageVo = new PageVo(articleList, total);
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult updateLink(LinkDTO linkDTO) {
        Link link = BeanCopyUtils.copyBean(linkDTO, Link.class);
        boolean b = updateById(link);
        if(b){
            return ResponseResult.okResult();
        }else{
            throw new SystemException(AppHttpCodeEnum.CATEGORY_UPDATE_ERROR);
        }

    }
    }


